package org.assistant.csp.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import my.assistant.homecashmanager.account.data.AccountBase;
import my.assistant.homecashmanager.account.domain.AccountDomain;

import org.assistant.common.exception.ImplementNotFoundException;
import org.assistant.common.utils.ClassUtils;
import org.assistant.common.web.servlet.mvc.Controller;
import org.assistant.common.web.servlet.mvc.Page;
import org.assistant.common.web.servlet.mvc.ResultData;
import org.assistant.csp.data.CacheInSession;
import org.assistant.csp.data.Global;
import org.assistant.csp.data.UserSession;
import org.assistant.csp.domain.UserDomain;

public class LoginController extends Controller {
	private static final long serialVersionUID = 9120051756066988897L;
	private UserDomain userManager;
	private AccountDomain accountDomain;

	public LoginController() throws ImplementNotFoundException {
		this.userManager = ClassUtils.getInterfaceImplement(UserDomain.class);
		this.accountDomain = ClassUtils
			.getInterfaceImplement(AccountDomain.class);
	}

	/**
	 * 请求分发器，在该函数中实现请求的转发
	 */
	protected Object requestDispatcher(HttpServletRequest request,
			HttpServletResponse response, String methodName) {
		try {
			if ("doLogin".equals(methodName)) {
				return doLogin(request, response);
			} else {
				return index(request, response);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 显示登录页面
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public Page index(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		return new Page("/frame/auth/login.jsp");
	}

	/**
	 * 登录系统
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ResultData<?> doLogin(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		System.out.println("\t doLogin ...");
		ResultData<?> resultData = null;

		String login = request.getParameter("login");
		String password = request.getParameter("password");

		if (login == null || "".equals(login.trim())) {
			String msg = "{'flag':'0','msg':'请输入登录账号！'}";
			resultData = new ResultData<String>(msg);
			return resultData;
		}
		if (password == null || "".equals(password.trim())) {
			String msg = "{'flag':'0','msg':'请输入登录密码！'}";
			resultData = new ResultData<String>(msg);
			return resultData;
		}

		boolean exist = userManager.isLoginAccountExist(login);
		if (!exist) {
			String msg = "{'flag':'0','msg':'用户不存在！'}";
			resultData = new ResultData<String>(msg);
			return resultData;
		}

		// String encryptPwd = StringUtil.encodePassword(password);
		String encryptPwd = password;

		boolean correct = userManager.isPasswordCorrect(login, encryptPwd);
		if (!correct) {
			String msg = "{'flag':'0','msg':'密码不正确！'}";
			resultData = new ResultData<String>(msg);
			return resultData;
		}

		// 登录
		UserSession userSession = userManager.getUserByLoginAccount(login);

		// 设置缓存信息
		List<AccountBase> accountList = null;
		if ("10001".equals(userSession.getRoleId())) {
			accountList = accountDomain.getAccountBaseListByHome(userSession
				.getHomeId());
		} else {
			accountList = accountDomain.getAccountBaseListByUser(userSession
				.getId());
		}
		CacheInSession cache = new CacheInSession();
		for (AccountBase account : accountList) {
			cache.putAccount(account.getId(), account);
		}
		userSession.setCacheInSession(cache);

		// 将用户信息放到session中
		request.getSession().setAttribute(Global.CURRENT_USER, userSession);

		String msg = "{'msg':'登录成功！'}";
		resultData = new ResultData<String>(msg);
		return resultData;
	}
}
